
package artistic;

import com.sun.java.swing.plaf.windows.resources.windows_de;
import com.sun.org.apache.xerces.internal.impl.dv.xs.YearDV;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.net.URL;
import javax.swing.Icon;
import javax.swing.ImageIcon;

/**
 *
 * @author Mikael Andersson
 * @author Rurik Högfeldt
 */

public class AirbrushTool implements PaintTool
{
    private ToolBox toolBox;
    private UpdateMe updater;

    private class UpdateMe implements Runnable
    {
        private boolean running = true;

        private long updateTime;
        private final long updateSpeed = 100;

        private AirbrushTool parent;
        private PaintWindow paintWindow;
        private int x, y;
        public UpdateMe(PaintWindow window, AirbrushTool parent, int x, int y)
        {
            this.paintWindow = window;
            this.parent = parent;
            this.x = x;
            this.y = y;
        }

        public void setPos(int x, int y)
        {
            this.x = x;
            this.y = y;
            parent.draw(paintWindow, x, y);
        }

        public void run() {
            updateTime = System.currentTimeMillis();
            while(running)
            {
                long systemTime = System.currentTimeMillis();
                if(systemTime - updateTime > updateSpeed)
                {
                    parent.draw(paintWindow, x, y);
                    updateTime = systemTime;
                }
            }
        }

        public void stop()
        {
            running = false;
        }
    }

    private void beginDraw(PaintWindow window, int x, int y)
    {
        if(updater==null)
        {
            updater = new UpdateMe(window, this, x, y);
            Thread thread = new Thread(updater);
            thread.start();
        }
    }

    private void endDraw()
    {
        if(updater!=null)
        {
            updater.stop();
            updater = null;
        }
    }

    public AirbrushTool(ToolBox toolBox)
    {
        this.toolBox = toolBox;
    }

    public Icon getIcon() {
        URL url = getClass().getResource("images/airbrushicon.png");
        return new ImageIcon(url);
    }

    private int lastX, lastY;

    //Used for all mouse input but mouse wheel
    public void mouseCallback(PaintWindow target, MouseEvent e) {
        if(e.getID() == MouseEvent.MOUSE_RELEASED)
        {
            endDraw();
            return;
        }
        if(e.getID() == MouseEvent.MOUSE_PRESSED)
        {
            int x = e.getX();
            int y = e.getY();
            beginDraw(target, x, y);
            updater.setPos(x, y);
        }
        if(e.getID() != MouseEvent.MOUSE_DRAGGED)
            return;

        int x = e.getX();
        int y = e.getY();
        updater.setPos(x, y);
    }

    //Used for mouse wheel input_
    public void mouseWheelCallback(PaintWindow target, MouseWheelEvent e) {

    }

    public void draw(PaintWindow target, int x, int y)
    {
        Brush brush = toolBox.getActiveBrush();
        
        PaintImage image = target.getImage();
        WorkCanvas canvas = image.createWorkLayer();

        brush.draw(canvas, x, y);

        canvas.done();

        target.repaint();
    }

}
